package com.example.game.app.controller;

import com.example.game.app.domain.*;
import com.example.game.module.entity.Category;
import com.example.game.module.entity.Game;
import com.example.game.module.service.CategoryService;
import com.example.game.module.service.GameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/game")
public class GameController {

    @Autowired
    private GameService gameService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public GameListVO getGameList(@RequestParam Integer page, @RequestParam(required = false) String keyword) {
        int pageSize = 5;

        List<Game> games = gameService.getAllGames(page, pageSize, keyword);
        Boolean isEnd = games.size() < pageSize;

        // 收集所有categoryId
        List<Long> categoryIds = games.stream()
                .map(Game::getCategoryId)
                .distinct()
                .collect(Collectors.toList());

        // 批量查询分类信息
        List<Category> categories = categoryService.listByIds(categoryIds);
        Map<Long, Category> categoryMap = categories.stream()
                .collect(Collectors.toMap(Category::getId, Function.identity()));

        List<GameItemVO> gameItems = games.stream()
                .map(game -> {
                    GameItemVO itemVO = new GameItemVO();
                    itemVO.setId(game.getId());
                    itemVO.setImage(game.getCoverImages());
                    itemVO.setGameName(game.getGameName());
                    itemVO.setPlayerNumber(game.getPlayerNumber());
                    Long categoryId = game.getCategoryId();
                    Category category = categoryMap.get(categoryId);
                    if (category != null) {
                        itemVO.setCategoryName(category.getName());
                    } else {
                        // 处理分类不存在的情况
                        itemVO.setCategoryName("未知分类");
                    }
                    return itemVO;
                })
                .collect(Collectors.toList());

        GameListVO gameListVO = new GameListVO();
        gameListVO.setList(gameItems);
        gameListVO.setIsEnd(isEnd);

        return gameListVO;
    }

    @GetMapping("/info")
    public GameInfoVo getGameInfo(@RequestParam Long gameId) {
        Game game = gameService.getById(gameId);

        if (game == null) {
            return null;
        }

        Category category = categoryService.getById(gameId);
        String categoryName = category != null ? category.getName() : null;
        String categoryImage = category != null ? category.getImage() : null;

        GameInfoVo gameInfoVO = new GameInfoVo();
        gameInfoVO.setTitle(game.getTitle());
        gameInfoVO.setCreateTime(game.getCreateTime());
        gameInfoVO.setCoverImages(Collections.singletonList(game.getCoverImages()));
        gameInfoVO.setGameName(game.getGameName());
        gameInfoVO.setReleaseDate(game.getReleaseDate());
        gameInfoVO.setPrice(game.getPrice());
        gameInfoVO.setGameDescription(game.getGameDescription());
        gameInfoVO.setRating(game.getRating());
        gameInfoVO.setCategoryName(categoryName);
        gameInfoVO.setCategoryImage(categoryImage);

        return gameInfoVO;
    }

    @GetMapping("/category")
    public CategoryListVO getCategoryList() {
        List<Category> categories = categoryService.findAll();

        List<CategoryTreeVO> tree = categories.stream()
                .filter(c -> c.getParentId() == null) // 筛选父分类
                .map(parent -> {
                    // 创建父节点 VO
                    CategoryTreeVO parentVO = new CategoryTreeVO();
                    parentVO.setId(parent.getId());
                    parentVO.setImage(parent.getImage());
                    parentVO.setName(parent.getName());


                    // 收集子分类并映射为 ItemVO
                    List<CategoryItemVO> children = categories.stream()
                            .filter(child -> parent.getId().equals(child.getParentId()))
                            .map(child -> new CategoryItemVO(
                                    child.getId(),
                                    child.getImage(),
                                    child.getName()
                            ))
                            .collect(Collectors.toList());

                    parentVO.setChildren(children);
                    return parentVO;
                })
                .collect(Collectors.toList());

        CategoryListVO categoryListVO = new CategoryListVO();
        categoryListVO.setList(tree);

        return categoryListVO;
    }

}